<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-left">
      <div class="JNPF-common-title">
        <h2>{{$t('common.organization')}}</h2>
        <span class="options">
          <el-dropdown>
            <el-link icon="icon-ym icon-ym-mpMenu" :underline="false" />
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="getOrganizeList()">刷新数据</el-dropdown-item>
              <el-dropdown-item @click.native="toggleExpand(true)">展开全部</el-dropdown-item>
              <el-dropdown-item @click.native="toggleExpand(false)">折叠全部</el-dropdown-item>
              <el-dropdown-item @click.native="showDiagram">架构图</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </span>
      </div>
      <div class="JNPF-common-tree-search-box">
        <el-input placeholder="输入关键字" v-model="filterText" suffix-icon="el-icon-search" clearable />
      </div>
      <el-scrollbar class="JNPF-common-el-tree-scrollbar" v-loading="treeLoading">
        <el-tree ref="treeBox" :data="treeData" :props="defaultProps" :default-expand-all="expands"
          highlight-current :expand-on-click-node="false" node-key="id"
          @node-click="handleNodeClick" class="JNPF-common-el-tree" v-if="refreshTree"
          :filter-node-method="filterNode">
          <span class="custom-tree-node" slot-scope="{ data, node }" :title="data.fullName">
            <i :class="data.icon" />
            <span class="text" :title="data.fullName">{{node.label}}</span>
          </span>
        </el-tree>
      </el-scrollbar>
    </div>
    <div class="JNPF-common-layout-center JNPF-flex-main">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item :label="$t('common.keyword')">
              <el-input v-model="listQuery.keyword" :placeholder="$t('common.enterKeyword')"
                clearable @keyup.enter.native="search()" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别">
              <el-select v-model="listQuery.gender" placeholder="请选择性别">
                <el-option v-for="item in genderTreeData" :key="item.enCode" :label="item.fullName"
                  :value="item.enCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="listQuery.enabledMark" placeholder="请选择状态">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
                <el-option label="锁定" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()">
                {{$t('common.search')}}</el-button>
              <el-button icon="el-icon-refresh-right" @click="reset()">{{$t('common.reset')}}
              </el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <TopButtons @add="addOrUpdateHandle()" @export="exportData()" @import="importUpload()">
            <el-button type="text" icon="icon-ym icon-ym-synForThird"
                       @click="handleSync">第三方同步</el-button>
          </TopButtons>

          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false"
                @click="initData()" />
            </el-tooltip>
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="tableData">
          <el-table-column prop="account" label="账号" width="100" />
          <el-table-column prop="realName" label="姓名" width="100" />
          <el-table-column prop="gender" label="性别" width="90" align="center" />
          <el-table-column prop="mobilePhone" label="手机" width="120" />
          <el-table-column prop="organize" label="所属组织" min-width="200" show-overflow-tooltip />
          <el-table-column prop="creatorTime" label="创建时间" :formatter="jnpf.tableDateFormat"
            width="140" />
          <el-table-column prop="sortCode" label="排序" width="70" align="center" />
          <el-table-column prop="enabledMark" label="状态" width="70" align="center">
            <template slot-scope="scope">
              <el-tag type="success" disable-transitions v-if="scope.row.enabledMark == 1">启用
              </el-tag>
              <el-tag type="warning" disable-transitions v-else-if="scope.row.enabledMark == 2">锁定
              </el-tag>
              <el-tag type="danger" disable-transitions v-else>禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope" v-if="!scope.row.isAdministrator">
              <tableOpts @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
                <el-dropdown hide-on-click>
                  <span class="el-dropdown-link">
                    <el-button size="mini" type="text">
                      {{$t('common.moreBtn')}}<i class="el-icon-arrow-down el-icon--right"></i>
                    </el-button>
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="handleResetPwd(scope.row.id, scope.row.account)">
                      {{$t('user.resetPassword')}}
                    </el-dropdown-item>
                    <el-dropdown-item @click.native="unlockUser(scope.row.id)"
                      v-if="scope.row.enabledMark == 2">解除锁定</el-dropdown-item>
                    <el-dropdown-item @click.native="socialsBindBtn(scope.row)" v-if="useSocials">
                      绑定管理
                    </el-dropdown-item>
                    <el-dropdown-item @click.native="workHandover(scope.row)"
                      :disabled="scope.row.enabledMark == 1 || scope.row.enabledMark == 2">
                      工作交接
                    </el-dropdown-item>
                    <el-dropdown-item @click.native="checkPermissions(scope.row)">
                      查看权限</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </tableOpts>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination :total="total" :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize" @pagination="initData" />
      </div>
    </div>
    <Form v-show="formVisible" ref="Form" @close="removeForm" />
    <Diagram v-if="diagramVisible" ref="Diagram" @close="diagramVisible = false" />
    <ResetPwdForm v-if="resetFormVisible" ref="ResetPwdForm" @refreshDataList="initData" />
    <ExportForm v-if="exportFormVisible" ref="exportForm" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="exportDownload"/>
    <ImportBox v-if="uploadBoxVisible" ref="UploadBox" @refresh="initData" :importHeader="this.exportList" />
    <ImportForm v-if="importFormVisible" ref="importForm" @refresh="reset()" />
    <SocialsBind v-if="socialsVisible" ref="SocialsBind" @close="socialsVisible=false" />
    <WorkHandoverForm v-if="workHandoverVisible" ref="WorkHandoverForm" @complete="initData" />
    <CheckPermissions v-if="checkPermissionsVisible" ref="checkPermissions"
      @close="checkPermissionsVisible=false" />
  </div>
</template>
<script>
import { getDepartmentSelectorByAuth } from '@/api/permission/department'
import { getUserList, updateUserState, unlockUser, delUser,userExport } from '@/api/permission/user'
import Form from './Form'
import Diagram from './Diagram'
import ResetPwdForm from './ResetPassword'
import ImportForm from './ImportForm'
import ExportForm from './ExportForm'
import SocialsBind from './SocialsBind'
import ExportBox from '../excel/ExportBox/index.vue'
import ImportBox from '../excel/ImportBox/index.vue'
import TopButtons from '@/views/permission/topButtons/index'
import WorkHandoverForm from './WorkHandoverForm.vue'
import { mapGetters } from "vuex"
import CheckPermissions from '@/components/CheckPermissions'
export default {
  name: 'permission-user',
  components: {
    Form,
    Diagram,
    ResetPwdForm,
    ExportForm,
    ImportForm,
    SocialsBind,
    WorkHandoverForm,
    CheckPermissions,
    ExportBox,
    ImportBox,
    TopButtons
  },
  data() {
    return {
      exportBoxVisible: false,
      uploadBoxVisible: false,
      exportList: [
        {prop:"account", label:"*账户",__config__:{jnpfKey:"input"}},
        {prop:"realName", label:"*姓名",__config__:{jnpfKey:"input"}},
        {prop:"gender", label:"*性别",__config__:{jnpfKey:"input"}},
        {prop:"email", label:"电子邮箱",__config__:{jnpfKey:"input"}},
        {prop:"organizeId", label:"*所属组织",__config__:{jnpfKey:"organizeSelect"}},
        {prop:"managerId", label:"直属主管",__config__:{jnpfKey: "select"}},
        {prop:"positionId", label:"岗位",__config__:{jnpfKey:"select"}},
        {prop:"ranks", label:"职级",__config__:{jnpfKey:"select"}},
        {prop:"roleId", label:"角色",__config__:{jnpfKey:"select"}},
        {prop:"nation", label:"民族",__config__:{jnpfKey:"input"}},
        {prop:"nativePlace", label:"籍贯",__config__:{jnpfKey:"input"}},
        {prop:"entryDate", label:"入职时间",__config__:{jnpfKey:"datePicker"}},
        {prop:"certificatesType", label:"证件类型",__config__:{jnpfKey:"input"}},
        {prop:"certificatesNumber", label:"证件号码",__config__:{jnpfKey:"input"}},
        {prop:"education", label:"文化程度",__config__:{jnpfKey:"select"}},
        {prop:"birthday", label:"出生年月",__config__:{jnpfKey: "datePicker"}},
        {prop:"telePhone", label:"办公电话",__config__:{jnpfKey:"input"}},
        {prop:"landline", label:"办公座机",__config__:{jnpfKey:"input"}},
        {prop: 'mobilePhone', label: '手机号码',  __config__:{jnpfKey:"input"} },
        {prop:"urgentContacts", label:"紧急联系",__config__:{jnpfKey:"input"}},
        {prop:"urgentTelePhone", label:"紧急电话",__config__:{jnpfKey:"input"}},
        {prop:"postalAddress", label:"通讯地址",__config__:{jnpfKey: "input"}},
        {prop:"enabledMark", label:"*状态",__config__:{jnpfKey:"switch"}},
        {prop:"sortCode", label:"排序",__config__:{jnpfKey:"inputNumber"}},
        {prop:"description", label:"说明",__config__:{jnpfKey:"textarea"}},
      ],
      genderTreeData: [],
      keyword: '',
      treeData: [],
      tableData: [],
      treeLoading: false,
      listLoading: true,
      listQuery: {
        organizeId: '',
        keyword: '',
        gender: '',
        enabledMark: '',
        currentPage: 1,
        pageSize: 20
      },
      defaultProps: {
        children: 'children',
        label: 'fullName'
      },
      total: 0,
      type: '',
      formVisible: false,
      diagramVisible: false,
      resetFormVisible: false,
      authorizeFormVisible: false,
      importFormVisible: false,
      exportFormVisible: false,
      expands: true,
      refreshTree: true,
      filterText: '',
      socialsVisible: false,
      workHandoverVisible: false,
      organizeIdTree: [],
      checkPermissionsVisible: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.treeBox.filter(val)
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    useSocials() {
      return localStorage.getItem('useSocials') && localStorage.getItem('useSocials') != '0'
    },
    menuId() {
      return this.$route.meta.modelId || ''
    }
  },
  created() {
    this.getOrganizeList(true)
  },
  methods: {
    importUpload() {
      this.uploadBoxVisible = true
      this.$nextTick(() => {
        this.$refs.UploadBox.init('', 'permission/Users', 0, [])
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.exportList, [])
      })
    },
    exportDownload(data) {
      let query = {...this.listQuery, dataType: data.dataType, selectKey: data.selectKey.join(","), menuId: this.menuId }
      userExport(query).then(res => {
        if (!res.data.url) return
        this.jnpf.downloadFile(res.data.url)
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    workHandover(data) {
      this.workHandoverVisible = true
      this.$nextTick(() => {
        this.$refs.WorkHandoverForm.init(data)
      })
    },
    showDiagram() {
      this.diagramVisible = true
      this.$nextTick(() => {
        this.$refs.Diagram.init()
      })
    },
    checkPermissions(item) {
      this.checkPermissionsVisible = true
      this.$nextTick(() => {
        item = { ...item, type: 'user', fullName: item.realName }
        this.$refs.checkPermissions.init(item, 2)
      })
    },
    search() {
      this.listQuery.currentPage = 1
      this.listQuery.pageSize = 20
      this.listQuery.sort = 'desc'
      this.initData()
    },
    reset() {
      this.listQuery.keyword = ''
      this.listQuery.gender = ''
      this.listQuery.enabledMark = ''
      this.search()
    },
    toggleExpand(expands) {
      this.filterText = ''
      this.refreshTree = false
      this.expands = expands
      this.$nextTick(() => {
        this.refreshTree = true
        this.$nextTick(() => {
          this.$refs.treeBox.setCurrentKey(this.companyId)
        })
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.fullName.indexOf(value) !== -1;
    },
    getOrganizeList(isInit) {
      this.filterText = ''
      this.treeLoading = true
      // 获取性别
      this.$store.dispatch('base/getDictionaryData', { sort: 'sex' }).then(res => {
        this.genderTreeData = res
      })
      getDepartmentSelectorByAuth().then(res => {
        this.treeData = res.data.list
        this.treeLoading = false
        if (isInit) this.initData()
      }).catch(() => {
        this.treeLoading = false
      })
    },
    initData() {
      this.listLoading = true
      getUserList(this.listQuery).then(res => {
        this.tableData = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    getNodePath(node) {
      let fullPath = []
      const loop = (node) => {
        if (node.level) fullPath.unshift(node.data)
        if (node.parent) loop(node.parent)
      }
      loop(node)
      return fullPath
    },
    handleNodeClick(data, node) {
      if (this.listQuery.organizeId === data.id) return
      this.listQuery.organizeId = data.id
      this.type = data.type
      const nodePath = this.getNodePath(node)
      this.organizeIdTree = nodePath.map(o => o.id)
      this.reset()
    },
    addOrUpdateHandle(id) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.Form.init(id, this.organizeIdTree || [])
      })
    },
    removeForm(isRefresh) {
      this.formVisible = false
      if (isRefresh) {
        this.keyword = ''
        this.initData()
      }
    },
    removeAuthorizeForm(isRefresh) {
      this.authorizeFormVisible = false
      if (isRefresh) {
        this.keyword = ''
        this.initData()
      }
    },
    handleUpdateState(row) {
      const txt = row.enabledMark ? '禁用' : '开启'
      this.$confirm(`您确定要${txt}当前用户吗, 是否继续?`, '提示', {
        type: 'warning'
      }).then(() => {
        updateUserState(row.id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            duration: 1000,
            onClose: () => {
              row.enabledMark = row.enabledMark ? 0 : 1
            }
          })
        })
      }).catch(() => { })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        delUser(id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            duration: 1500,
            onClose: () => {
              this.initData()
            }
          })
        })
      }).catch(() => { })
    },
    unlockUser(id) {
      this.$confirm('此操作将解除该账户锁定, 是否继续?', '解除锁定', {
        type: 'warning'
      }).then(() => {
        unlockUser(id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            duration: 1500,
            onClose: () => {
              this.initData()
            }
          })
        })
      }).catch(() => { })
    },
    exportForm() {
      this.exportFormVisible = true
      this.$nextTick(() => {
        this.$refs.exportForm.init(this.listQuery)
      })
    },
    uploadForm() {
      this.importFormVisible = true
      this.$nextTick(() => {
        this.$refs.importForm.init()
      })
    },
    handleResetPwd(id, account) {
      this.resetFormVisible = true
      this.$nextTick(() => {
        this.$refs.ResetPwdForm.init(id, account)
      })
    },
    socialsBindBtn(data) {
      this.socialsVisible = true
      this.$nextTick(() => {
        this.$refs.SocialsBind.init(data)
      })
    },
    handleSync() {
      this.$router.push({ path: `/system/sysConfig?type=1` })
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .icon-ym {
  font-size: 12px !important;
}
</style>
